package org.yaac.server.egql.evaluator.function;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.yaac.server.egql.evaluator.EvaluationResult;
import org.yaac.server.egql.exception.EGQLException;
import org.yaac.server.egql.processor.ProcessData.ProcessDataRecord;
import org.yaac.shared.ErrorCode;
import org.yaac.shared.property.PropertyType;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Category;
import com.google.appengine.api.datastore.Email;
import com.google.appengine.api.datastore.GeoPt;
import com.google.appengine.api.datastore.IMHandle;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Link;
import com.google.appengine.api.datastore.PhoneNumber;
import com.google.appengine.api.datastore.PostalAddress;
import com.google.appengine.api.datastore.ShortBlob;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.users.User;
import com.google.common.collect.ImmutableMap;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class TypeFunction extends Function {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// this map should be in enum PropertyType
	// however so many classes in this map can't be compiled by GWT, put it here as a workaround
	private static final Map<Class<?>, String> TYPE_MAP =  ImmutableMap.<Class<?>, String>builder().put(
			String.class, PropertyType.STRING.getRepresentation())
			.put(Date.class, PropertyType.TIMESTAMP.getRepresentation())
			.put(Boolean.class, PropertyType.BOOL.getRepresentation())
			.put(User.class, PropertyType.USER.getRepresentation())
			.put(BigDecimal.class, "decimal")	// not a property type
			.put(BlobKey.class, PropertyType.BLOB_KEY.getRepresentation())
			.put(Blob.class, PropertyType.BLOB.getRepresentation())
			.put(Text.class, PropertyType.TEXT.getRepresentation())
			.put(Key.class, PropertyType.KEY.getRepresentation())
			.put(GeoPt.class, PropertyType.GEOPT.getRepresentation())
			.put(IMHandle.class, PropertyType.IM_HANDLE.getRepresentation())
			.put(List.class, PropertyType.LIST.getRepresentation())
			.put(ShortBlob.class, PropertyType.SHORT_BLOB.getRepresentation())
			.put(PostalAddress.class, PropertyType.POSTTAL_ADDRESS.getRepresentation())
			.put(PhoneNumber.class, PropertyType.PHONE_NO.getRepresentation())
			.put(Link.class, PropertyType.LINK.getRepresentation())
			.put(Email.class, PropertyType.EMAIL.getRepresentation())
			.put(Category.class, PropertyType.CATEGORY.getRepresentation())
			.build();
	
	@Override
	public void validate() throws EGQLException {
		FunctionUtil.ensureParamSize(ops, 1);
	}
	
	@Override
	public EvaluationResult evaluate(ProcessDataRecord record) {
		if (this.ops.isEmpty()) {
			return new EvaluationResult(null).withWarning(ErrorCode.W115);
		} else {
			EvaluationResult r = ops.get(0).evaluate(record);
			if (r.getPayload() == null) {
				return new EvaluationResult(PropertyType.NULL.getRepresentation(), r);
			} else {
				return new EvaluationResult(TYPE_MAP.get(r.getPayload().getClass()), r);
			}
		}
	}
}
